<?php 
namespace Common\Model;

/**
 * 客栈预定端掌柜说信息表
 * 
 * @index hotelId(`hotelentity_id`)
 */

Class HotelSpecialModel extends \Common\Model\BaseModel
{
	protected $trueTableName = 'bk_hotel_special';
	protected $field = 'id,hotelentity_id,special,specialimageurl,isactive,isdelete,createtime,updatetime,sortindex';

	/**
     * 添加客栈个性图片信息
     * @modify wenhui 2016-10-28 创建
     * @param  integer   $hotelId        客栈id
     * @param  string   $url   个性图片url
     */
	public function addInfo($hotelId, $url) {
		$data = array(
			'hotelentity_id' => $hotelId,
			'specialimageurl' => $url,
		);

		return $this->add($data);
	}
	/**
     * 修改图片和图片说明信息
     * @modify wenhui 2016-10-28 创建
     * @param  integer   $hotelId        客栈id
     * @param  string 	 $special 		 个性图片描述
     * @param  string   $specialId   个性图片id
     * @param  string   $url   个性图片url
     */
	public function saveInfo($hotelId, $specialId, $url = null, $special = null, $index = null) {
		$option = array(
			'id' => $specialId,
			'hotelentity_id' => $hotelId,
		);
		$data = array();
		if (!is_null($url)) {
			$data['specialimageurl'] = $url;
		}

		if (!is_null($special)) {
			$data['special'] = $special;
		}

		if (!is_null($index)) {
			$data['sortindex'] = $index;
		}

		return $this->where($option)->save($data) === false ? false : ture;
	}
	/**
     * 删除个性推荐信息
     * @modify wenhui 2016-10-28 创建
     * @param  integer   $hotelId   客栈ID
     * @param  integer   $specialId  个性推荐图片id
     * @return boolean              执行结果
     */
	public function deleteInfo($specialId) {
		$data = array(
			'id' => $specialId,
			'isdelete' => IS_DELETE,
		);

		return $this->save($data) === false ? false : ture;
	}
	/**
     * 保存 掌柜说
     * @modify wenhui 2016-10-28 创建
     * @param  integer   $hotelId   客栈ID
     * @param  integer   $specialId  个性推荐图片id
     * @return boolean              执行结果
     */
	public function saveManage($specialId, $oldIndex = null) {

		if (is_array($specialId)) {
			$specialId = $specialId[0];
		}

		$data = array(
			'id' => $specialId,
			'isactive' => ENABLE,
		);

		if (!is_null($oldIndex)) {
			$data['sortindex'] = $oldIndex;
		}

		return $this->save($data) === false ? false : ture;
	}
	/**
     * 根据$specialId获取个性图片信息
     * @modify wenhui 2016-10-28 创建
     * @modify wenhui 2017-03-24 支持获取多个
     * @param  mixed   $specialId  个性推荐图片id
     * @return array     $specialInfo      执行结果
     */
	public function getSpecialInfoById($specialId) {

		if (is_array($specialId)) {
			$specialId = implode(',', $specialId);
		}

		if (strpos($specialId, ',') == false) {
			$option['id'] = $specialId;
			return $this->where($option)->field($this->field)->find();
		} else {
			$option['id'] = array('in', $specialId);
			return $this->where($option)->field($this->field)->select();
		}
	}
	/**
     * 初始化掌柜说 默认为零
     * @modify wenhui 2016-10-28 创建
     * @return boolean              执行结果
     */
	public function saveInit($hotelId, $index = null) {
		$data = array(
			'isactive' => 0,
		);
		if (!is_null($index)) {
			$data['sortindex'] = $index;
		}
		$option = array(
			'hotelentity_id' => $hotelId,
			'isactive' => 1,
		);
		
		return $this->where($option)->save($data) === false ? false : ture;
	}
	/**
     * 根据$hotelId获取多套个性推荐信息
     * @modify wenhui 2016-10-28 创建
     * @return array              执行结果
     */
	public function getInfoByHotel($hotelId) {
		$option = array(
			'hotelentity_id' => $hotelId,
			'isdelete' => DISABLE,
		);		
		$data = $this->where($option)->field($this->field)->select();

        return empty($data) ? array() : $data;				
	}
	/**
     * 获取客栈预定端个性推荐信息
     * @modify wenhui 2016-10-28 创建
     * @return array              执行结果
     */
	public function getSpecials($hotelId) {
		$option = array(
			'hotelentity_id' => $hotelId,
			'isactive' => ENABLE,
		);		
		$data = $this->where($option)->field($this->field)->find();

        return empty($data) ? array() : $data;				
	}
}